<?php

namespace app\api\controller;

use app\admin\model\Auditlog;
use app\admin\model\CouponRecord;
use app\admin\model\Details;
use app\admin\model\Products;
use app\admin\model\Worktags;
use app\api\middleware\RoleCheck;
use app\api\service\OrderService;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Db;
use think\Hook;
use think\Request;
use think\Validate;

/**
 * 经销商 代理操作
 */
class Agent extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }


    //入驻
    public function settled(){
        $data = input();
        $auditlogvalidator = new \app\admin\validate\Auditlog();
        if (!$auditlogvalidator->check($data)) {
            $this->error('修改失败', $auditlogvalidator->getError());
        }
        $data['applicant_id']=$this->auth->id;
        $phone=\app\admin\model\Details::where('phone','=',$data['phone'])->count();
        if($phone>0){
            $this->error('此手机号已申请');
        }
        $count=\app\admin\model\Auditlog::where('phone','=', $data['phone'])->where('audit_status','=','待审核')->count();
        if($count>0){
            $this->error('有待审核的记录，请勿重复提交');
        }

        $result=\app\admin\model\Auditlog::create($data);
        if($result){
            $this->success('提交成功，等待审核');
        }else{
            $this->error('提交失败');
        }
    }




    /*
     * 陪伴室人员统计
     */
    public function room_child() {
        $uid = $this->auth->id;
        $name = input('name'); // 假设这是输入的姓名，可以在请求中传递
        $query = Details::where(function ($query) use ($uid) {
            $query->whereIn('user_id', function ($subQuery) use ($uid) {
                $subQuery->table('fa_user')
                    ->where('room_id', '=', $uid)
                    ->where('login_status', '=', '正常')
                    ->field('id');
            })
                ->WhereOr(function ($query) use ($uid) {
                    $query->whereNull('user_id')
                        ->where('applicant_id', '=', $uid);
                });
        });


        // 如果 name 有值，则添加 like 查询
        if (!empty($name)) {
            $query = $query->where('name', 'like', "%{$name}%");
        }




        // 获取最终结果
        $list = $query->field('name, "星陪伴师" as role')->select();

        $this->success('请求成功',$list);
    }
    /*
     *代理人员统计
     */
    public function agent_child(){
        $uid=$this->auth->id;
        $role=input('role');
        $name=input('name');
        //查询user表 此代理下的用户 和Details表入驻未注册的用户
        $list = Details::where(function ($query) use ($uid, $role) {
            $query->whereIn('user_id', function ($subQuery) use ($uid, $role) {
                $subQuery->field('id')
                    ->table('fa_user')
                    ->where('agent_id', '=', $uid)
                    ->where('login_status', '=', '正常')
                    ->where('role', '=', $role);
            })
                ->WhereOr(function ($query) use ($uid, $role) {
                    $query->whereNull('user_id')
                        ->where('role', '=', $role)
                        ->where('applicant_id', '=', $uid);
                });
        });
        if (!empty($name)) {
            $list = $list->where('name', 'like', '%' .$name. '%');
        }

        $list = $list->field('id,name,phone,role,user_id,applicant_id')->select();

        foreach ($list as $k => $v) {

            if (!empty($v['name'])) {
                $firstChar = mb_substr($v['name'], 0, 1); // 保留姓
                $maskedName = $firstChar . '**';
                $list[$k]['name'] = $maskedName;
            }

            if ($role == 'companion') {
                //如果用户还没注册 申请人就是上级
//                if($v['user_id']==null){
//                    $name = \app\admin\model\Details::where('user_id', '=', $v['applicant_id'])->value('name');
//                }else{
//                    //注册的话 去user表里查询上级
//                    $p_info = \app\admin\model\User::where('id', '=', $v['user_id'])->find();
//                    if($p_info['agent_id']!=null){
//                        $room_id=$p_info['agent_id'];
//                    }else{
//                        $room_id=$p_info['room_id'];
//                    }
//                        $name = \app\admin\model\Details::where('user_id', '=', $room_id)->value('name');
//                }
                $list[$k]['p_name'] = '*****';
                $list[$k]['role'] = '陪伴师';
            }else{
                $list[$k]['role'] = '星陪伴室';
            }
        }

        //陪伴室人员统计
        $room = Details::where(function ($query) use ($uid) {
            $query->whereIn('user_id', function ($subQuery) use ($uid) {
                $subQuery->table('fa_user')
                    ->where('agent_id', '=', $uid)
                    ->where('role', '=', 'room')
                    ->field('id');
            })->whereOr(function ($query) use ($uid) {
                $query ->whereNull('user_id')
                    ->where('role', '=', 'room')
                ->where('applicant_id', '=', $uid);
            });
        })->count();
        //陪伴师人员统计
        $companion = Details::where(function ($query) use ($uid) {
            $query->whereIn('user_id', function ($subQuery) use ($uid) {
                $subQuery->table('fa_user')
                    ->where('agent_id', '=', $uid)
                    ->where('login_status', '=', '正常')
                    ->where('role', '=', 'companion')
                    ->field('id');
            })->whereOr(function ($query) use ($uid) {
                $query ->whereNull('user_id')
                    ->where('role', '=', 'companion')
                ->where('applicant_id', '=', $uid);
            });
        })->count();


        $data['room']=$room;
        $data['companion']=$companion;
        $data['data']=$list;
        $this->success('请求成功',$data);

    }

   //合同内容
    public function my_contract(){

        $user=\app\admin\model\User::where('id','=',$this->auth->id)->find();
        if($user['role']=='agent'){
            $type='Conagent';
        }else if($user['role']=='companion'){
            $type='Concom';
        }else if($user['role']=='room'){
            $type='Conroom';
        }else{
            $this->error('该用户无合同');
        }
        $content=\app\admin\model\Article::where('type','=',$type)->value('content');
        $details=Details::where('user_id','=',$this->auth->id)->find();
        $data['content']=$content;
        $data['signature_url']=$details['signature_url']==null?'':$details['signature_url'];
        $this->success('请求成功',$data);
    }


}
